﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Library;
namespace QuanLyThi
{
    public partial class FrmCauHinhMucDoCauHoi : Form
    {
        public static string mConnect = FrmMain.mConnect;
        private string msql = "";
        public DGC_Library mLibrary = new DGC_Library(mConnect);

        public FrmCauHinhMucDoCauHoi()
        {
            InitializeComponent();
            this.Load();
            //comboBox1_SelectionChangeCommitted(null, null);
        }
        private void button6_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void Load()
        {
            msql = "Select * from tb_Capdokho";
            DataTable dt = mLibrary.GetDataTable(msql, "table");
            cloai.DataSource = dt;
            cloai.DisplayMember = "tencapdo";
            cloai.ValueMember = "ms_capdo"; 
        }

        private void cchuongtrinhdt_SelectionChangeCommitted(object sender, EventArgs e)
        {
            msql = "Select a.ms_Mondaotao,a.mondaotao,a.ghichu from tb_Mondaotao a inner join tb_Cauhinhcapdo " +
                   " b on a.MS_Mondaotao=b.MS_Mondaotao where B.ms_capdo='"+cloai.SelectedValue.ToString()+"' Order by Mondaotao";                  
            DataTable dt1 = mLibrary.GetDataTable(msql, "table");
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = dt1;


            msql = " Select a.ms_Mondaotao,a.mondaotao,a.ghichu from TB_mondaotao a  where a.MS_Mondaotao " +
                   " not in ( Select MS_Mondaotao from tb_Cauhinhcapdo where ms_capdo='" + cloai.SelectedValue.ToString() + "' ) Order by a.Mondaotao";
            DataTable dt2 = mLibrary.GetDataTable(msql, "table");
            dataGridView2.AutoGenerateColumns = false;
            dataGridView2.DataSource = dt2;
        }

  

        private void butup_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.dataGridView2.RowCount > 0)
                {
                    //them row phia duoi
                    DataTable dt = (DataTable)dataGridView1.DataSource;
                    DataRow newrow = dt.NewRow();
                    newrow[0] = dataGridView2.CurrentRow.Cells[0].Value.ToString();
                    newrow[1] = dataGridView2.CurrentRow.Cells[1].Value.ToString();
                    newrow[2] = dataGridView2.CurrentRow.Cells[2].Value.ToString();
                    dt.Rows.Add(newrow);
                    //Delete row phia trên
                    DataGridViewRow row = dataGridView2.CurrentRow;
                    dataGridView2.Rows.Remove(row);
                    //DataTable dt2 = (DataTable)dataGridView2.DataSource;
                    //dt2.Rows[dataGridView2.CurrentRow.Index].Delete();


                    //dataGridView2.DataSource = dt2;
                }
            }
            catch (Exception)
            {

                MessageBox.Show("Xem lại,có lỗi xãy ra", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void butdown_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.dataGridView1.RowCount > 0)
                {
                    //them row phia duoi
                    DataTable dt = (DataTable)dataGridView2.DataSource;
                    DataRow newrow = dt.NewRow();
                    newrow[0] = dataGridView1.CurrentRow.Cells[0].Value.ToString();
                    newrow[1] = dataGridView1.CurrentRow.Cells[1].Value.ToString();
                    newrow[2] = dataGridView1.CurrentRow.Cells[2].Value.ToString();
                    dt.Rows.Add(newrow);
                    //Delete row phia trên
                    dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
                    //DataTable dt2 = (DataTable)dataGridView1.DataSource;
                    //dt2.Rows[dataGridView1.CurrentRow.Index].Delete();
                    //dataGridView1.DataSource = dt2;
                }
            }
            catch (Exception)
            {

                MessageBox.Show("Xem lại,có lỗi xãy ra", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


        private void butLuu_Click(object sender, EventArgs e)
        {
            try
            {
                DialogResult dialog = MessageBox.Show("Bạn có muốn lưu thông tin này không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (dialog == DialogResult.Yes)
                {                  
                    //Xóa thông tin củ
                    msql = "Delete tb_cauhinhcapdo Where ms_capdo='" + cloai.SelectedValue.ToString() + "'";
                    mLibrary.RunProc(msql);
                    //cập nhật thông tin mới
                    DataTable dt = (DataTable)dataGridView1.DataSource;
                    if (dt.Rows.Count > 0)
                    {
                        for (int i = 0; i < dt.Rows.Count ; i++)
                        {
                            string msmon="";
                            try
                            {
                                msmon = dt.Rows[i]["MS_monDaoTao"].ToString();
                                msql = "  Insert into tb_Cauhinhcapdo (Ms_capdo,MS_Mondaotao,stamptime) values (" + cloai.SelectedValue.ToString() + " ," + msmon + ",getdate())";
                                mLibrary.RunProc(msql);   
                            }
                            catch (Exception)
                            {
                                string a = "";
                            }
                                                   
                        }
                    }                    
                    MessageBox.Show("Lưu thông tin thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Lưu thông tin không thành công", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void butLuu_Click_1(object sender, EventArgs e)
        {
            butLuu_Click(null, null);
        }          
     
    }
}
